package com.wangzhe.builder;

import com.wangzhe.bean.Constants;
import com.wangzhe.bean.FieldInfo;
import com.wangzhe.bean.TableInfo;
import com.wangzhe.utils.DateTimeUtil;
import com.wangzhe.utils.StringUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.util.List;
import java.util.Map;

/**
 * @author 王哲
 * @ClassName BuilderMapper
 * @create 2023--六月--下午1:22
 * @Description 构造Mapper类
 * @Version V1.0
 */
public class BuilderMapper {

    private static Logger logger = LoggerFactory.getLogger(BuilderMapper.class);

    public static void builderMapper(TableInfo tableInfo) {
        File folder = new File(Constants.MAPPER_PACKAGE_PATH);
        if (!folder.exists()) {
            folder.mkdirs();
        }
        String beanName = tableInfo.getBeanName() + Constants.MAPPER_SUFFIX;
        File file = new File(folder, beanName + ".java");

        OutputStream os = null;
        OutputStreamWriter osw = null;
        BufferedWriter bw = null;

        try {
            os = new FileOutputStream(file);
            osw = new OutputStreamWriter(os, "utf-8");
            bw = new BufferedWriter(osw);

            bw.write("package " + Constants.MAPPER_PACKAGE + "; ");
            bw.newLine();
            bw.newLine();

//            bw.write("import " + Constants.MAPPER_PACKAGE + ";");
//            bw.newLine();
            bw.write("import org.apache.ibatis.annotations.Param;");
            bw.newLine();

            if (tableInfo.isHaveDate() || tableInfo.isHaveDateTime()) {
                bw.write("import java.util.Date;");
                bw.newLine();
            }

            for (FieldInfo fieldInfo : tableInfo.getFieldInfo()) {
                if (ArrayUtils.contains(Constants.DB_IGNORE_BEAN_FIELD, fieldInfo.getPropertyName())) {
                    bw.write(Constants.DB_IGNORE_BEAN_IMPORT);
                    bw.newLine();
                    break;
                }
            }
            bw.newLine();
            bw.newLine();
            BuilderComment.createClassComment(bw, tableInfo.getTableName() + "表的Mapper类");

            bw.write("public interface " + beanName + "<T, P> extends BaseMapper {");
            bw.newLine();
            bw.newLine();

            //拿到主键
            Map<String, List<FieldInfo>> keyIndexMap = tableInfo.getKeyIndexMap();
            for (Map.Entry<String, List<FieldInfo>> stringListEntry : keyIndexMap.entrySet()) {
                List<FieldInfo> value = stringListEntry.getValue();

                Integer index = 0;
                StringBuilder sb = new StringBuilder();
                StringBuilder methodParams = new StringBuilder();

                for (FieldInfo fieldInfo : value) {
                    index++;
                    sb.append(StringUtils.upperCaseFirst(fieldInfo.getPropertyName()));
                    if (index < value.size()) {
                        sb.append("And");
                    }
                    methodParams.append("@Param(\"" + fieldInfo.getPropertyName() + "\") " + fieldInfo.getFieldJavaType() + " " + fieldInfo.getPropertyName());
                    if (index < value.size()) {
                        methodParams.append(",");
                    }
                }
                //查询语句生成
                BuilderComment.createMethodComment(bw, "根据" + sb.toString() + "查询");
                bw.write("\t T selectBy" + sb.toString() + "(" + methodParams + ");");
                bw.newLine();

                //删除语句生成
                BuilderComment.createMethodComment(bw, "根据" + sb.toString() + "删除");
                bw.write("\t Integer deleteBy" + sb.toString() + "("+ methodParams +");");
                bw.newLine();

                //更新语句生成
                BuilderComment.createMethodComment(bw, "根据" + sb.toString() + "更新");
                bw.write("\t Integer updateBy" + sb.toString() + "(@Param(\"bean\") T t, "+methodParams+");");
                bw.newLine();
            }

            bw.newLine();
            bw.write("}");
            bw.flush();
        } catch (Exception e) {
            logger.info("创建Mapper失败:{}", e.getMessage());
        } finally {
            try {
                if (bw != null) {
                    bw.close();
                }
                if (osw != null) {
                    osw.close();
                }
                if (os != null) {
                    os.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}
